<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      :inline="true"
      v-show="showSearch"
      label-width="88px"
    >
      <el-form-item label="企业名称" prop="enterpriseName">
        <el-input
          v-model="queryParams.enterpriseName"
          placeholder="请输入企业名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="企业法人" prop="legalPerson">
        <el-input
          v-model="queryParams.legalPerson"
          placeholder="请输入企业法人"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="企业联系人" prop="enterpriseContact">
        <el-input
          v-model="queryParams.enterpriseContact"
          placeholder="请输入企业联系人"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['energy:qyEnterprise:add']"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="addApart"
          v-hasPermi="['life:feLeaseRecord:add']"
          >公寓租赁</el-button
        >
      </el-col>
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="danger"-->
<!--          plain-->
<!--          icon="el-icon-plus"-->
<!--          size="mini"-->
<!--          @click="addCom"-->
<!--          v-hasPermi="['life:feLeaseRecord:add']"-->
<!--          >企业增租</el-button-->
<!--        >-->
<!--      </el-col>-->

      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleImport"
          v-hasPermi="['energy:qyEnterprise:add']"
        >导入
        </el-button>
      </el-col>

      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['energy:qyEnterprise:edit']"
          >修改</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['energy:qyEnterprise:remove']"
          >删除</el-button
        >
      </el-col>





      <!-- <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['energy:qyEnterprise:export']"
          >导出</el-button
        >
      </el-col> -->
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>


    <el-table
      v-loading="loading"
      :data="qyEnterpriseList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column
        label="企业名称"
        show-overflow-tooltip
        align="center"
        prop="enterpriseName"
      />
      <el-table-column label="企业法人" align="center" prop="legalPerson" />
      <el-table-column label="法人联系电话" align="center" prop="legalPhone" />
      <el-table-column
        label="企业联系人"
        align="center"
        prop="enterpriseContact"
      />
      <el-table-column
        label="企业联系人电话"
        align="center"
        prop="contactPhone"
      />
      <el-table-column
        label="企业类型"
        show-overflow-tooltip
        align="center"
        prop="enterType"
      />
      <el-table-column label="生产分类" align="center" prop="productionType" >
        <template slot-scope="scope">
<!--          <span v-for="(item,index) in JSON.parse(scope.row.productionType)" :key="index">-->
<!--            <template v-if="index==JSON.parse(scope.row.productionType).length-1">-->
<!--               {{item}}-->
<!--            </template>-->
<!--            <template v-else>{{item}}、</template>-->
<!--          </span>-->
          {{scope.row.productionType}}
        </template>
      </el-table-column>
      <el-table-column
        label="行业类型"
        show-overflow-tooltip
        align="center"
        prop="enterpriseType"
      />
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
        fixed="right"
        width="200"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-tickets"
            @click="handleLeasing(scope.row)"
            v-hasPermi="['energy:qyEnterprise:edit']"
            >租赁记录</el-button
          >
          <el-button
            icon="el-icon-plus"
            type="text"
            size="mini"
            @click="addCom(scope.row)"
            v-hasPermi="['life:feLeaseRecord:add']"
          >增租</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['energy:qyEnterprise:edit']"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['energy:qyEnterprise:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <el-dialog :title="upload.title" :visible.sync="upload.open" width="400px" append-to-body>
      <el-upload
        ref="upload"
        :limit="1"
        accept=".xlsx, .xls"
        :headers="upload.headers"
        :action="upload.url + '?updateSupport=' + upload.updateSupport"
        :disabled="upload.isUploading"
        :on-progress="handleFileUploadProgress"
        :on-success="handleFileSuccess"
        :auto-upload="false"
        drag
      >
        <i class="el-icon-upload"></i>
        <div class="el-upload__text">
          将文件拖到此处，或
          <em>点击上传</em>
        </div>
        <div class="el-upload__tip" slot="tip">
          <!--<el-checkbox v-model="upload.updateSupport" />是否更新已经存在的数据-->
          <el-link type="info" style="font-size:12px" @click="importTemplate">下载模板</el-link>
        </div>
        <div class="el-upload__tip" style="color:red" slot="tip">提示：仅允许导入“xls”或“xlsx”格式文件！</div>
      </el-upload>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitFileForm">确 定</el-button>
        <el-button @click="upload.open = false">取 消</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
import {
  listQyEnterprise,
  getQyEnterprise,
  delQyEnterprise,
  addQyEnterprise,
  updateQyEnterprise,
} from "@/api/smartParks/merchants/merchantsCompanySettlein";
import { listBuiRoom } from "@/api/parkAssets/buildingRoom";
import { getToken } from '@/utils/auth'
export default {
  name: "QyEnterprise",
  components: {},
  data() {
    return {
      upload: {
        // 是否显示弹出层
        open: false,
        // 弹出层标题
        title: "",
        // 是否禁用上传
        isUploading: false,
        // 是否更新已经存在的用户数据
        updateSupport: 0,
        // 设置上传的请求头部
        headers: { Authorization: getToken() },
        // 上传的地址
        url: process.env.VUE_APP_BASE_API + "/energy/qyEnterprise/importData"
      },

      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 企业概况表格数据
      qyEnterpriseList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 企业类型字典
      enterpriseTypeOptions: [],
      buiRoomList: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        enterpriseName: null,
        legalPerson: null,
        legalPhone: null,
        enterpriseContact: null,
        contactPhone: null,
        enterpriseType: null,
        registrationDate: null,
        taxRegistrationDate: null,
        approvalDate: null,
        acceptanceDate: null,
        productionStatus: null,
        address: null,
      },
      // 表单参数
      form: {
        roomIdList: [],
      },
      selectionArr: [],
      // 表单校验
      rules: {
        enterpriseName: [
          { required: true, message: "企业名称不能为空", trigger: "blur" },
        ],
        legalPerson: [
          { required: true, message: "企业法人不能为空", trigger: "blur" },
        ],
        legalPhone: [
          { required: true, message: "法人联系电话不能为空", trigger: "blur" },
        ],
        enterpriseContact: [
          { required: true, message: "企业联系人不能为空", trigger: "blur" },
        ],
        contactPhone: [
          {
            required: true,
            message: "企业联系人电话不能为空",
            trigger: "blur",
          },
        ],
        enterpriseType: [
          { required: true, message: "企业类型不能为空", trigger: "change" },
        ],
        delFlag: [
          {
            required: true,
            message: "删除标志(0正常 1删除)不能为空",
            trigger: "blur",
          },
        ],
      },
    };
  },
  created() {
    this.getList();
    this.getDicts("enterprise_type").then((response) => {
      this.enterpriseTypeOptions = response.data;
    });
  },
  methods: {
    /** 下载模板操作 */
    importTemplate() {
      this.download('/energy/qyEnterprise/importTemplate',{
        ...this.queryParams
      }, `企业入驻导入模板.xlsx`)
    },
    /** 导入按钮操作 */
    handleImport() {
      this.upload.title = '入驻信息导入'
      this.upload.open = true
    },
    // 文件上传中处理
    handleFileUploadProgress(event, file, fileList) {
      this.upload.isUploading = true
    }
    ,
// 文件上传成功处理
    handleFileSuccess(response, file, fileList) {
      this.upload.open = false
      this.upload.isUploading = false
      this.$refs.upload.clearFiles()
      this.$alert(response.msg, '导入结果', { dangerouslyUseHTMLString: true })
      this.getList()
    },

// 提交上传文件
    submitFileForm() {
      this.$refs.upload.submit()
      this.upload.open = false

    },

    addApart() {
      if (this.ids.length === 0) {
        this.msgError("请选择一条数据");
        return;
      }
      if (this.ids.length > 1) {
        this.msgError("只能选择一条数据");
        return;
      }
      const id = this.ids[0];

      this.$router.push({
        name: "QypartmentAdd",
        query: { enId: id, name: this.selectionArr[0].enterpriseName },
      });
    },
    handleLeasing(row) {
        this.$router.push({
        name: "Leasing",
        query: { id: row.id }
      });
        // query: { id: row.id },

    },
    addCom(row) {
      const id = row.id;
      console.log(id)
      this.$router.push({
        name: "MerchantsCompanySettleinAdd",
        query: { id: id },
      });
    },
    /** 查询企业概况列表 */
    getList() {
      this.loading = true;
      listQyEnterprise(this.queryParams).then((response) => {
        this.qyEnterpriseList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 行业类型字典翻译
    enterpriseTypeFormat(row, column) {
      return this.selectDictLabel(
        this.enterpriseTypeOptions,
        row.enterpriseType
      );
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        enterpriseName: null,
        legalPerson: null,
        legalPhone: null,
        enterpriseContact: null,
        contactPhone: null,
        enterpriseType: null,
        registrationDate: null,
        mainProduct: null,
        enterpriseScale: null,
        taxRegistrationDate: null,
        approvalDate: null,
        acceptanceDate: null,
        createBy: null,
        productionStatus: "0",
        createTime: null,
        address: null,
        updateBy: null,
        updateTime: null,
        remark: null,
        delFlag: null,
        tenantId: null,
        roomIdList: [],
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.selectionArr = selection;
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.$router.push({ name: "MerchantsCompanySettleinAdd" });
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
       const id = row.id || this.ids;
      this.$router.push({ name: "QyEnterpriseAdd", query: { id:id } });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            updateQyEnterprise(this.form).then((response) => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addQyEnterprise(this.form).then((response) => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm("是否确认删除企业概况的数据项?", "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(function () {
          return delQyEnterprise(ids);
        })
        .then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "energy/qyEnterprise/export",
        {
          ...this.queryParams,
        },
        `energy_qyEnterprise.xlsx`
      );
    },
  },
};
</script>
